package com.caiyi.financial.nirvana.sms.controller;

import com.youyu.common.api.Result;
import com.youyu.common.enums.BaseResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Api(description = "通用接口")
@CrossOrigin
@RestController
@RequestMapping("sms/admin/common")
public class CommonController {


    @ApiOperation(value = "查找Enum详情", notes = "查找Enum详情描述")
    @PostMapping("/enum")
    public Result toIndexDescriptionMap(String em) {
        Result result = new Result();
        List<Map<Object, Object>> list = new ArrayList<>();
        try {
            Class clazz = Class.forName("com.caiyi.financial.nirvana.sms.enums." + em);
            Method getIndex = clazz.getMethod("getIndex");
            Method getDesc = clazz.getMethod("getDesc");
            //得到enum的所有实例
            Object[] objList = clazz.getEnumConstants();
            for (Object obj : objList) {
                LinkedHashMap map = new LinkedHashMap();
                map.put("index", getIndex.invoke(obj));
                map.put("desc", getDesc.invoke(obj));
                list.add(map);
            }
            result.setCode(BaseResultCode.SUCCESS.getCode());
            result.setDesc(BaseResultCode.SUCCESS.getDesc());
            //result.setData(CommUtil.map2Object(map));
            result.setData(list);
        } catch (Exception e) {
            result.setCode(BaseResultCode.SYSTEM_ERROR.getCode());
            result.setDesc(BaseResultCode.SYSTEM_ERROR.getDesc());
        }
        return result;
    }
}

